package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.CompanyEventsChildListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CompanyEvents;
import com.isunland.managesystem.entity.CompanyEventsChild;
import com.isunland.managesystem.entity.CompanyEventsChildOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:  公司大事记子表列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/18 10:32
 */
public class CompanyEventsChildListFragment extends BaseListFragment {

  private CompanyEvents mContent;
  private ArrayList<CompanyEventsChild> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContent =
        (CompanyEvents) getArguments().getSerializable(CompanyEventsListFragment.EXTRA_VALUE);
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_COMPANY_EVENTS_CHILDLIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("mainId", mContent.getId());
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    CompanyEventsChildOriginal original =
        new Gson().fromJson(json, CompanyEventsChildOriginal.class);
    ArrayList<CompanyEventsChild> rows = original.getRows();
    if (Base.SUCCESS == original.getResult() && rows != null) {
      mList.clear();
      mList.addAll(rows);
      CompanyEventsChildListAdapter mAdapter =
          new CompanyEventsChildListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    CompanyEventsChild content = mList.get(position - 1);
    Intent i = new Intent(getActivity(), CompanyEventsChildActivity.class);
    i.putExtra(CompanyEventsChildDetailFragment.EXTRA_VALUE, content);
    startActivity(i);
  }

  public static CompanyEventsChildListFragment newInstance(CompanyEvents content) {
    Bundle args = new Bundle();
    args.putSerializable(CompanyEventsListFragment.EXTRA_VALUE, content);
    CompanyEventsChildListFragment fragment = new CompanyEventsChildListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
